package com.pic4493.app;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

import com.pic4493.base.BaseUI;
import com.pic4493.comm.Session;
import com.pic4493.controls.CstZoomImageView;
import com.pic4493.utils.UtiDialog;
import com.pic4493.utils.UtiFile;

public class UIPic extends BaseUI
{

   private ImageView mImgBack;
   private ImageView mImgDownload;
   private boolean isSave=false;

   private CstZoomImageView mImgPic;

   private void initView()
   {
	  mImgPic = (CstZoomImageView) findViewById(R.id.uipic_cst_img);
      mImgBack=(ImageView)findViewById(R.id.uipic_img_back);
      mImgDownload=(ImageView)findViewById(R.id.uipic_img_download);
   }

   private void initEvent()
   {
	  mImgBack.setOnClickListener(new View.OnClickListener()
	  {
	     @Override
	     public void onClick(View v)
	     {
	  	    finish();
	     }
	  });
	  mImgDownload.setOnClickListener(new View.OnClickListener()
	  {  
	     @Override
	     public void onClick(View v)
	     {
	  	    try
			{
			   if (!isSave)
			   {
				  UtiFile.saveMyBitmap(mContext, Session.getInstance().picCache);
				  isSave = true;
			   }
			   else
			   {
				  UtiDialog.getInstance().ShowToastInfoShort(mContext, "您已经保存过该图片");
			   }
			}
			catch (Exception e)
			{
			   Log.d("ERROR", "UIPic.initEvent().new OnClickListener() {...}_onClick(v) " + e);
			}
	     }
	  });
   }

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
	  setContentView(R.layout.uipic);
	  super.onCreate(savedInstanceState);

	  initView();
	  initEvent();

	  mImgPic.setImageBitmap(Session.getInstance().picCache);
   }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
	  if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
	  { // 按下的如果是BACK，同时没有重复
		 finish();
	  }
	  return false;
   }
   
   @Override
   protected void onDestroy()
   {
	  super.onDestroy();
	  try
	  {
		 Session.getInstance().picCache = null;
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "UIPic_onDestroy() " + e);
	  }
   }
}
